package com.huatai.exchange.conn;

import com.alibaba.druid.pool.DruidDataSource;
import com.faneui.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;

@Component
public class LocalConnection {

    private static final Logger logger = LoggerFactory.getLogger(LocalConnection.class);

    @Autowired
    private DruidDataSource druidDataSource;

    private Connection conn;

    public Connection getConn() throws BusinessException {
        try {
            if (conn == null || conn.isClosed()) {
                synchronized (LocalConnection.class) {
                    if (null == conn || conn.isClosed()) {
                        conn = druidDataSource.getConnection().getConnection();
                    }
                }
            }
        } catch (Exception ex) {
            logger.error("", ex);
            throw new BusinessException(ex.getMessage());
        }
        return conn;
    }
}
